//
//  XNStatusOriginalview.swift
//  weibo
//
//  Created by 王向宁 on 16/5/24.
//  Copyright © 2016年 王向宁. All rights reserved.
//原创微博视图

import UIKit
import SnapKit
import SDWebImage

let StatusCellMargin: CGFloat = 8
class XNStatusOriginalview: UIView {
    
    var bottomCons: Constraint?

    var statusViewModel : XNStatusViewModel? {
        didSet {
            
//            let url = NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? "")
            
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            
            nameLabel.text = statusViewModel?.status?.user?.name
            
            timeLabel.text = statusViewModel?.status?.created_at
            
            sourceLabel.text = statusViewModel?.status?.source
            
            contentLabel.text = statusViewModel?.status?.text
            
            verifiedImageView.image = statusViewModel?.veritiedImage
            
            mbrankImageView.image = statusViewModel?.mbrankImage
            
//            pictureView.pictures = statusViewModel?.status?.pic_urls
            //设置数据前根据图片的数量来判断是否需要调整自己底部的约束
            //在更新约束前需要先卸载之前的记录约束
            bottomCons?.uninstall()
            
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                pictureView.pictures = urls
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                pictureView.hidden = false
            } else {
                pictureView.pictures = nil
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.yellowColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        addSubview(headIcon)
        
        addSubview(verifiedImageView)
        
        addSubview(nameLabel)
        
        addSubview(mbrankImageView)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        //设置约束
        headIcon.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusCellMargin)
            
            make.height.equalTo(40)
            
            make.width.equalTo(40)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) in
        
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            
            make.left.equalTo(headIcon.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            make.left.equalTo(contentLabel.snp_left)
            
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    
    //懒加载
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var timeLabel:UILabel = UILabel(title: "11:11", font: 10, textColor: UIColor.orangeColor())
    
    private lazy var sourceLabel:UILabel = UILabel(title: "王大锤", font: 10, textColor: UIColor.darkGrayColor())
    
    private lazy var nameLabel:UILabel = UILabel(title: "杜磊", font: 14, textColor: UIColor.darkGrayColor())
    
    private lazy var contentLabel:UILabel = UILabel(title: "我是王大锤,我为自己带盐,我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    
    private lazy var pictureView: XNXNStatusPictureView = XNXNStatusPictureView()
    

}
